﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class map_in : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private int m_NodeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_NodeId = QueryStringHelper.GetInteger("node", 0);

        if (!IsPostBack)
        {
            if (m_NodeId == 0)
            {
                Response.Redirect("map.aspx");
            }

            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            if (nodeVO != null)
            {
                ltlArea.Text = nodeVO.Name;
                FillList();
            }
        }
    }

    private void FillList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("ParentNodeId", m_NodeId.ToString());
        conditions.Add("Flag", "1");

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        ltlCount.Text = AspNetPager1.RecordCount.ToString();
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        RepeaterList.DataSource = m_PostService.GetPostList(conditions);
        RepeaterList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillList();
    }

    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            NodeVO node = m_PostService.GetNodeById(postVO.Node.NodeId);
            UIHelper.SetLiteralText(ctrl, "ltlCity", node.Name);
            if (1.Equals(postVO.Type) && !string.IsNullOrEmpty(postVO.PicFileName))
            {
                UIHelper.SetLiteralText(ctrl, "ltlType", GetFile(postVO.PicFileName));
            }
            else
            {
                UIHelper.SetLiteralText(ctrl, "ltlType", "尚無開課資訊");
            }
        }
    }

    private string GetFile(string fileName)
    {
        return string.Format("<a href='upload/{0}' target='_blank'>開課資訊</a>", fileName);
    }
}